using UnityEngine;

/// <summary>
/// 控制直升机旋翼旋转行为的脚本
/// </summary>
public class BladesController : MonoBehaviour
{
    /// <summary>
    /// 可选的旋转轴枚举
    /// </summary>
    public enum Axis { X, Y, Z }

    [Tooltip("启用时旋翼将反向旋转")]
    [SerializeField] private bool inverseRotation = false;

    [Tooltip("选择旋翼的旋转轴")]
    [SerializeField] private Axis axis = Axis.X;

    [Tooltip("旋转速度的乘数系数")]
    [SerializeField] private float _speedMultiplier = 1f;

    // 当前计算出的旋转轴向量
    private Vector3 _rotationAxis;
    private float _directionFactor; //旋转方向因素
    
    // 当前旋翼转速（经过Clamp限制）
    private float _bladeSpeed;

    /// <summary>
    /// 获取或设置旋翼转速（自动限制在0-3000范围内）
    /// </summary>
    public float BladeSpeed
    {
        get => _bladeSpeed;
        set => _bladeSpeed = Mathf.Clamp(value, 0, 3000);
    }

    /// <summary>
    /// 初始化时根据选择的轴更新旋转轴向量
    /// </summary>
    private void Awake()
    {
        _directionFactor = inverseRotation ? -1 : 1;

        //根据当前axis枚举值更新实际的旋转轴向量
        switch (axis)
        {
            case Axis.Y:
                _rotationAxis = Vector3.up;
                break;
            case Axis.Z:
                _rotationAxis = Vector3.forward;
                break;
            case Axis.X:
            default:
                _rotationAxis = Vector3.right;
                break;
        } 
    }

    /// <summary>
    /// 每帧更新旋翼旋转
    /// 计算最终旋转速度（考虑反向和乘数）
    /// 使用本地坐标系进行旋转
    /// </summary>
    void Update()
    {
        float currentSpeed = _directionFactor * _bladeSpeed * _speedMultiplier;
        transform.Rotate(_rotationAxis, currentSpeed * Time.deltaTime, Space.Self);
    }
}